import pygame, math, sys
from pygame.locals import *

versao = 0.1

tamanho = (largura, altura) = 285, 396

pygame.init()

tela = pygame.display.set_mode(tamanho)

fundo = pygame.image.load('imagens/mapa.jpg').convert_alpha()
pygame.display.set_caption("The pycmen game - versao %0.1f" % versao)
pygame.mouse.set_visible(0)

clock = pygame.time.Clock()

FRAMES_PER_SECOND = 30

class PycmanSprite(pygame.sprite.Sprite):

    def __init__( self ):
        pygame.sprite.Sprite.__init__( self )
        self.image     = pygame.image.load('imagens/pacman_right.png').convert_alpha()
        self.rect      = self.image.get_rect()
        self.direction = 270
        self.speed     = 2 
        self.moves()

    def moves(self):
        x, y = self.get_pos()
        rad  = self.direction * math.pi / 180
        x   += -self.speed * math.sin( rad )
        y   += -self.speed * math.cos( rad )
        self.set_pos( ( x, y ) )
        print ( x, y )

    def get_pos( self ):
        return ( self.rect.center )
    
    def set_pos( self, pos ):
        self.rect.center = pos

    def set_dir( self, dir ):
        self.direction = dir

    def change_image( self, img ):
        self.image = pygame.image.load(img);

    def rotate_image( self, angle ):
        self.image = pygame.transform.rotate( self.image, angle )


#

pac = PycmanSprite()

PAC_IMAGE_RIGHT = 'imagens/pacman_right.png'
PAC_IMAGE_LEFT  = 'imagens/pacman_left.png'
PAC_IMAGE_UP    = 'imagens/pacman_up.png'
PAC_IMAGE_DOWN  = 'imagens/pacman_down.png'

pac_new_image = PAC_IMAGE_RIGHT

running = True

k_dir = 270
rot_x = 0
rot_y = 0

while running:

    clock.tick( FRAMES_PER_SECOND )
    # ENTRADA DO USUARIO
    for event in pygame.event.get():
        if not hasattr( event, 'key' ):
            continue
        # if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
        # if event.type == pygame.KEYDOWN:
        down = event.type == KEYDOWN # 1 se apertou, 0 se soltou
        
        if event.key == K_UP:
            print 'vai pra cima!'
            pac_new_image = PAC_IMAGE_UP
            k_dir = 0
    
        if event.key == K_DOWN:
            print 'vai pra baixo!'
            pac_new_image = PAC_IMAGE_DOWN
            k_dir = 180
    
        if event.key == K_RIGHT:
            print 'vai pra direita!'
            pac_new_image = PAC_IMAGE_RIGHT
            k_dir = 270
    
        if event.key == K_LEFT:
            print 'vai pra esquerda!'
            pac_new_image = PAC_IMAGE_LEFT
            k_dir = 90
    
        if event.key == K_SPACE:
            print 'espaco'
    
        if event.key == K_f:
            print 'f'
    
        if event.key == pygame.K_ESCAPE or event.type == pygame.QUIT:
            running = False
    
    # SIMULACAO
    pac.set_dir( k_dir )
    pac.moves( )

    pac.change_image( pac_new_image )

#
    # RENDERIZACAO
    tela.blit(fundo, (0,0))
    tela_center = tela.get_rect().center
    pacman      = pac.image
    rect        = pac.rect
    tela.blit(pacman, rect)
    pygame.display.flip()
#

